<?php

namespace App\Repositories\Eloquent\Act;

use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;

class JoinRepository extends Repository
{

    /**
     * @param App
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Act\Join';
    }


    public function index($type = '')
    {
        $result = $this->model->orderBy('id', 'asc')->get();

        return $result;
    }


    public function isAttend($request)
    {
        $rows = $this->model
            ->where('mobile',$request->mobile)
            ->where('topic_id',$request->topic_id)
            ->get();

        if($rows->count() > 0)
            return true;

        return false;
    }

    /**
     * 新春活动名单
     */
    public function lists()
    {
        $joins = $this->model->with('answer','question','topic')
                ->select(\DB::raw("`mobile`, COUNT(`mobile`) as c, `name`, `created_at` "))
                ->groupBy('mobile','name')
                ->orderBy('created_at')
                ->get();       
        foreach($joins as $join){
            $ids = $this->model->where('mobile',$join->mobile)->get('id')->pluck('id')->toArray();
            $answers = \App\Models\Act\Answer::with('question','topic')->whereIn('join_id',$ids)->get();
            $join->answers = $answers;
            //dump($ids);
        }
        //dd("ok");
        return $joins;
    }


}